import Vue from 'vue'
import VueRouter from 'vue-router'
import ManagerTask from "@/components/manager/ManagerTask.vue";
import ManagerHead from "@/components/manager/ManagerHead.vue";
import ProjectList from "@/components/ProjectList.vue";
import MGeneralProject from "@/components/manager/MGeneralProject.vue";
import NewlyAdd from "@/components/manager/NewlyAdd.vue";
import ProjectDoing from "@/components/ProjectDoing.vue";
import ProjectDone from "@/components/ProjectDone.vue";
import LoginView from "@/views/LoginView.vue";
import ManagerMember from "@/components/manager/ManagerMember.vue";
import Register from "@/views/Register.vue";
import MemberHead from "@/components/member/MemberHead.vue";
import CheckerHead from "@/components/checker/CheckerHead.vue";
import CheckerProjectList from "@/components/checker/CheckerProjectList.vue";
import CheckerDoing from "@/components/checker/CheckerDoing.vue";
import CheckerDone from "@/components/checker/CheckerDone.vue";
import CheckerCompleted from "@/components/checker/CheckerCompleted.vue";
import CheckerMember from "@/components/checker/CheckerMember.vue";
import CheckPage from "@/components/checker/CheckPage.vue";
import MemberProjectList from "@/components/member/MemberProjectList.vue";
import MemberTask from "@/components/member/MemberTask.vue";
import ManagerChatView from "@/components/manager/ManagerChatView.vue";
import forgotPassword from "@/views/forgotPassword.vue";
import {Message} from "element-ui";
import CheckChartView from "@/components/checker/CheckChartView.vue";
import LineChart from "@/components/LineChart.vue";

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/Login'
  },
  {
    path: '/register',
    name: 'RegisterView',
    component: Register
  },
  {
    path: '/forgotPassword',
    name: 'forgotPassword',
    component: forgotPassword
  },
  {
    path: '/Login',
    name: 'Login',
    component:LoginView
  },
  {
    path:'/MemberHead',
    name: 'MemberHead',
    component:MemberHead,
    children: [
      {
        path: 'MemberProjectList',
        name: 'MemberProjectList',
        component: MemberProjectList,
        children: [
          {
            path: 'ProjectDoing',
            name: 'ProjectDoing',
            component: ProjectDoing
          },
          {
            path: 'ProjectDone',
            name: 'ProjectDone',
            component: ProjectDone
          },
        ]
      },
      {
        path: 'MemberTask',
        name: 'MemberTask',
        component: MemberTask,
      }
    ]
  },
  {
    path:'/CheckerHead',
    name: 'CheckerHead',
    component: CheckerHead,
    children:[
      {
        path: 'CheckerProjectList',
        name: 'CheckerProjectList',
        component: CheckerProjectList,
        children:[
          {
            path: 'CheckerDoing',
            name: 'CheckerDoing',
            component: CheckerDoing,
          },
          {
            path: 'CheckerDone',
            name: 'CheckerDone',
            component: CheckerDone,
          },
          {
            path: 'CheckerCompleted',
            name: 'CheckerCompleted',
            component: CheckerCompleted,
          }
        ]
      },
      {
        path: 'CheckerMember',
        name: 'CheckerMember',
        component: CheckerMember
      },
      {
        path: 'CheckChart',
        name: 'CheckChart',
        component: CheckChartView
      },
      {
        path: 'CheckPage',
        name: 'CheckPage',
        component: CheckPage
      },
    ]
  },
  {
    path: '/ManagerHead',
    name: 'ManagerHead',
    component: ManagerHead,
    children:[
      {
        path: 'ManagerTask',
        name: 'ManagerTask',
        component: ManagerTask
      },
      {
        path: 'Member',
        name: 'Member',
        component: ManagerMember
      },
      {
        path: 'ManagerChatView',
        name: 'ManagerChatView',
        component: ManagerChatView,
      },
      {
        path: 'ProjectList',
        name: 'ProjectList',
        component: ProjectList,
        children:[
          {
            path: 'ProjectDoing',
            name: 'ProjectDoing',
            component: ProjectDoing,
          },
          {
            path: 'ProjectDone',
            name: 'ProjectDone',
            component: ProjectDone,
          }
        ]
      },
      {
        path: 'AddProject',
        name: 'AddProject',
        component: NewlyAdd
      },
      {
        path: 'MGeneralProject',
        name: 'MGeneralProject',
        component: MGeneralProject
      },
      {
        path: '/LineChart',
        name: 'LineChart',
        component: LineChart
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const publicPages = ['/Login', '/register','/forgotPassword']; // 公开页面，不需要 token
  const authRequired = !publicPages.includes(to.path);
  const token = localStorage.getItem('token');
  const userType = localStorage.getItem('userType');//用户角色信息

  if (authRequired && !token) {
    if ((from.path === '/forgotPassword' || to.path === '/Login')||(from.path === '/register' || to.path === '/Login'))
    {
      return next();//正常跳转，不提示错误
    }

      console.log("1")
      Message.error('请先登录，再访问其他页面');
      return next('/Login'); // 统一跳转到登录页
  }

  // 如果用户已登录，检查用户角色是否匹配目标页面
  if (token) {
    if (to.path.includes('/ManagerHead') && userType !== '1') {
      Message.error('您没有访问该页面的权限，请重新登录');
      return next('/');
    }
    if (to.path.includes('/MemberHead') && userType !== '2') {
      Message.error('您没有访问该页面的权限，请重新登录');
      return next('/');
    }
    if (to.path.includes('/CheckerHead') && userType !== '3') {
      Message.error('您没有访问该页面的权限，请重新登录');
      return next('/');
    }
  }

  next();
});
export default router
